package com.vstudy.zx.dao.provider;

import com.vstudy.zx.utils.page.ArticlePage;
import com.vstudy.zx.utils.page.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

public class ArticleManageDaoProvider {
//        public String count(@Param("page") ArticlePage page, @Param("te_id") Long te_id,
//                @Param("grade_id") Integer grade_id, @Param("subject_id") Long subject_id){

    public String count(ArticlePage page) {
        String sql = new SQL() {{
            SELECT("count(*)");
            FROM("tb_article");
            if (page.getTe_id() != null) {
                WHERE("article_author = #{te_id}");
            }
            if (page.getTitle() != null) {
                WHERE("article_title like concat('%',#{title},'%')");
            }
            if (page.getSubject_id() != null && page.getGrade_id() == null) {
                WHERE("outline_id in(select o_id from tb_outline where subject_id=#{subject_id})");
            } else if (page.getGrade_id() != null && page.getSubject_id() == null) {
                WHERE("outline_id in(select o_id from tb_outline where grade_id=#{grade_id})");
            } else {
                WHERE("outline_id in(select o_id from tb_outline where subject_id=#{subject_id} and grade_id=#{grade_id})");
            }

        }}.toString();
        return sql;
    }

    public String selectPage(@Param("page") Page page, @Param("te_id") Long te_id, @Param("grade_id") Integer grade_id, @Param("subject_id") Long subject_id) {
        String sql = new SQL() {{
            SELECT("*");
            FROM("tb_article");
            if (te_id != null) {
                WHERE("article_author = #{te_id}");
            }

        }}.toString();
        sql += " limit #{page.skip}, #{page.pageSize}";
        return sql;
    }
}
